package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.common.dto.DingTalkReviewResultDTO;
import com.zmn.oms.common.dto.RedisReviewDTO;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.zmn.normal.business.interfaces.review.OmsOrderReviewBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName OmsOrderRefundReviewListener
 * @description:
 * @author: wangyanci
 * @create: 2021-08-10 19:50
 * @Version 1.0
 **/
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.dove}")
@Component
public class OmsOrderRefundReviewListener {

    @Autowired
    private OmsOrderReviewBService orderReviewBService;
    @Autowired
    RedisManager redisManager;

    @AliyunMQHandler(tags = OmsMqTagConsts.DING_TALK_AUDIT_PROCESS)
    public void reviewCallback(String message) {
        log.info("审批回调[{}]", message);
        DingTalkReviewResultDTO reviewResultDTO = JSON.parseObject(message, DingTalkReviewResultDTO.class);
        String key = String.format(RedisKeyConsts.ORDER_REVIEW_CODE_KEY, reviewResultDTO.getProcessInstanceId());
        String json = redisManager.get(key);
        if (StringUtil.isBlank(json)) {
            log.debug("redis中未找到！");
            return;
        }
        RedisReviewDTO redisReviewDTO = JSON.parseObject(json, RedisReviewDTO.class);
        try {
            orderReviewBService.reviewCallBack(reviewResultDTO, redisReviewDTO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("{}审批回调error：{}", redisReviewDTO.getWorkId(), e);
        }
    }

}